package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.CdcSystemEntity;
import com.dhcc.cdc.sys.service.ICdcSystemService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 系统信息
 * 
 * @ClassName: CdcSystemController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:18:52
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcsystem")
public class CdcSystemController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcsystem/";

	@Autowired
	private ICdcSystemService cdcSystemService;
	

    /**
     * 跳转到系统信息列表首页
     */
    @RequestMapping("")
    @Permission("cdcsystem:list")
    public String index() {
        return PREFIX + "cdcsystem.html";
    }
	
    /**
     * 跳转到新增系统信息
     */
    @RequestMapping("/to_add")
    @Permission("cdcsystem:add")
    public String toAdd() {
        return PREFIX + "cdcsystem_add.html";
    }
	
	/**
     * 跳转到修改系统信息
     */
    @RequestMapping("/to_update/{cdcSystemId}")
    @Permission("cdcsystem:update")
    public String toUpdate(@PathVariable Long cdcSystemId, Model model) {
        CdcSystemEntity cdcSystem = this.cdcSystemService.selectById(cdcSystemId);
        model.addAttribute("cdcSystem",cdcSystem);
        return PREFIX + "cdcsystem_edit.html";
    }

    /**
     * 获取系统信息列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcsystem:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcSystemEntity> page = new PageFactory<CdcSystemEntity>().defaultPage();
        List<CdcSystemEntity> result = this.cdcSystemService.page(page,params);
        page.setRecords((List<CdcSystemEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增系统信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcsystem:add")
    @BussinessLog(value = "新增系统信息")
    public Object add(CdcSystemEntity cdcSystem) {
        if (CommonUtil.isOneEmpty(cdcSystem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcSystem.insert();
        return R.ok();
    }

    /**
     * 删除系统信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcsystem:delete")
    @BussinessLog(value = "删除系统信息")
    public Object delete(@RequestParam Long cdcSystemId) {
	
        this.cdcSystemService.deleteById(cdcSystemId);

        return R.ok();
    }

    /**
     * 修改系统信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcsystem:update")
    @BussinessLog(value = "修改系统信息")
    public Object update(CdcSystemEntity cdcSystem) {
        if (CommonUtil.isOneEmpty(cdcSystem)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcSystemEntity old = this.cdcSystemService.selectById(cdcSystem.getId());

        old.setCode(cdcSystem.getCode());
        old.setDescription(cdcSystem.getDescription());
        old.setCustomer(cdcSystem.getCustomer());
        old.setVersion(cdcSystem.getVersion());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
